package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorHotServer;
import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.modelnobind.ProvideCommodityMove;
import guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove;
import guoanmaker.operator.business.repository.OperatorHotServerRepository;
import guoanmaker.operator.business.repository.OperatorServerClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

/**
 *  
 *
 * @ClassName: OperatorHotServerService 
 * @Description: OperatorHotServerService 
 * @author: huZhaoyu 
 * @date 2017/6/28 下午2:56 
 */
@Service
public class OperatorHotServerTypeService implements OperatorHotServerServiceInterface {
    private final OperatorHotServerRepository operatorHotServerRepository;
    private final OperatorServerClassRepository operatorServerClassRepository;
    @PersistenceContext
	private EntityManager manager;
    @Override
    public List<OperatorHotServer> getAllAndServerName(String cityId) {
        List<OperatorHotServer> servers = operatorHotServerRepository.findBycityCode(cityId);
        for(OperatorHotServer server:servers){
        	 OperatorServerClass serverClass=	operatorServerClassRepository.findOne(server.getServerId());
        	 server.setServerName(serverClass.getServerName());
        }
        return servers;
    }

    @Override
    public Integer countAllById() {
        return operatorHotServerRepository.countAllByIdIsNotNull();
    }

    @Override
    public Page<OperatorHotServer> getHotServerByPageOrderByWeight(Integer page, Integer size) {
        return operatorHotServerRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "weight")));
    }

    @Autowired
    public OperatorHotServerTypeService(OperatorHotServerRepository operatorHotServerRepository, OperatorServerClassRepository operatorServerClassRepository) {
        this.operatorHotServerRepository = operatorHotServerRepository;
        this.operatorServerClassRepository = operatorServerClassRepository;
    }

    @Override
    @Transactional
    public OperatorHotServer saveHotServer(OperatorHotServer operatorHotServer) {
        return operatorHotServerRepository.save(operatorHotServer);
    }

    @Override
    public List<OperatorHotServer> getAllHotServer() {
        return operatorHotServerRepository.findAll();
    }

    @Override
    public Page<OperatorHotServer> getAllHotServerByPage(Integer page, Integer size) {
        return operatorHotServerRepository.findAll(new PageRequest(page, size));
    }

    @Override
    @Transactional
    public void deleteHotServerById(String id) {
        operatorHotServerRepository.delete(id);
    }

    @Override
    public OperatorHotServer getHotServerById(String id) {
        return operatorHotServerRepository.findOne(id);
    }

	@Override
	public OperatorHotServer getHotServerByserverId(String serverId) {
		// TODO Auto-generated method stub
		return operatorHotServerRepository.findByserverId(serverId);
	}
	
	  @Override
	    public List<OperatorHotServer> getAllhotServerByNull(String cityId) {
		 /* String jpql="select new guoanmaker.operator.business.modelnobind.ProvideCommodityMov"
		  		+ "select u.serverId,u.iconPath  from OperatorHotServer u where u.cityId=11";
		  
  		Query query=manager.createQuery(jpql);//查询商品信息
  		
  		
			List<OperatorHotServer> servers= query.getResultList();*/
	        List<OperatorHotServer> servers = operatorHotServerRepository.findBycityCode(cityId);
	        for(OperatorHotServer server:servers){
	        	server.setId(null);
	        	 OperatorServerClass serverClass=	operatorServerClassRepository.findOne(server.getServerId());
	        	 server.setServerName(serverClass.getServerName());
	        }
	        return servers;
	    }
	  @Transactional
	  public void delectAllBycitycode(String cityId){
		  String jpql="delete  from OperatorHotServer  u where u.cityId=:cityId";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityId", cityId);
			query2.executeUpdate();
		  
	  }

}
